package com.aixingfu.sports;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aixingfu.sports.MVP.contract.RetrievePwdContract;
import com.aixingfu.sports.MVP.presenter.RetrievePwdPresenter;
import com.aixingfu.sports.Model.CodeModel;
import com.aixingfu.sports.Model.RetrievePwdModel;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.orie.librarybase.base.BaseActivity;
import com.orie.librarybase.utils.CountDownTimerUtils;
import com.vondear.rxtool.view.RxToast;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//忘记密码
@Route(path = "/main/ForgetPasswordActivity")
public class ForgetPasswordActivity extends BaseActivity implements RetrievePwdContract.MyView {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_getcode)
    TextView tvGetcode;

    @BindView(R.id.ed_account_number)
    EditText edAccountNumber;
    @BindView(R.id.ed_code)
    EditText edCode;
    @BindView(R.id.ed_new_pwd)
    EditText edNewPwd;
    @BindView(R.id.ed_confirm_pwd)
    EditText edConfirmPwd;
    @BindView(R.id.tv_change_pwd_true)
    TextView tvChangePwdTrue;
    @BindView(R.id.tv_change)
    TextView tvChange;
    private RetrievePwdPresenter retrievePwdPresenter;
    private String codedata;
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_forget_password;
    }

    public static void startActivityByRoute() {
        ARouter.getInstance().build("/main/ForgetPasswordActivity").navigation();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        tvTitle.setText("忘记密码");
        retrievePwdPresenter = new RetrievePwdPresenter();
        retrievePwdPresenter.attachView(this);
    }
    @Override
    protected void initEvent() {

    }
    @Override
    protected void beforeInit() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
    @OnClick({R.id.iv_back, R.id.tv_getcode, R.id.tv_change_pwd_true, R.id.tv_change})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_getcode:
                String accountNumber = edAccountNumber.getText().toString();
                CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(ForgetPasswordActivity.this, tvGetcode, 60000, 1000);
                countDownTimerUtils.start();
                getCode(accountNumber);
                break;
            case R.id.tv_change_pwd_true:
                String accountNumber1 = edAccountNumber.getText().toString();
                String code = edCode.getText().toString();
                String newPwd = edNewPwd.getText().toString();
                String confirmPwd = edConfirmPwd.getText().toString();
                if (TextUtils.isEmpty(accountNumber1)){
                    RxToast.normal(ForgetPasswordActivity.this,"请输入账号").show();
                }else if (TextUtils.isEmpty(code)){
                    RxToast.normal(ForgetPasswordActivity.this,"请输入验证码").show();
                }else if (TextUtils.isEmpty(newPwd)){
                    RxToast.normal(ForgetPasswordActivity.this,"请输入新密码").show();
                }else if (!newPwd.equals(confirmPwd)){
                    RxToast.normal(ForgetPasswordActivity.this,"两次输入密码不相同，请重新输入").show();
                }else {
                    getRetrievePwd(accountNumber1,code,newPwd);
                }
                break;
            case R.id.tv_change:
                finish();
                break;
        }
    }
    public void getCode(String accountNumber){
        if (retrievePwdPresenter.isViewAttached()){
            HashMap<String,String> map = new HashMap<>();
            map.put("mobile",accountNumber);
            map.put("type","findpwd");
            retrievePwdPresenter.getCode(map);
        }
    }
    public void getRetrievePwd(String accountNumber,String code,String newPwd){
        if (retrievePwdPresenter.isViewAttached()){
            HashMap<String,String> map = new HashMap<>();
            map.put("mobile",accountNumber);
            map.put("code",code);
            map.put("newpwd",newPwd);
            retrievePwdPresenter.getRetrievePwd(map);
        }
    }

    @Override
    public void showToast(String msg, String state) {
    }

    @Override
    public void setCode(CodeModel data) {
        if (data.getCode() == 1){
            codedata = String.valueOf(data.getData());
//            edCode.setText(codedata);
            RxToast.normal(ForgetPasswordActivity.this,data.getMessage()).show();
        }else {
            RxToast.normal(ForgetPasswordActivity.this,data.getMessage()).show();
        }
    }
    @Override
    public void setRetievePwd(RetrievePwdModel data) {
        if (data.getCode() == 1){
            RxToast.normal(ForgetPasswordActivity.this,data.getMessage()).show();
            finish();
        }else {
            RxToast.normal(ForgetPasswordActivity.this,data.getMessage()).show();
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public Context getMyContext() {
        return this;
    }
}
